import baseUrl from '@/request/config.js'

let request = (option)=>{
	//请求弹出loading框
	uni.showToast({
		title:'加载中...',
		mask:true,
	})
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseUrl + option.url,
			method:option.method || 'GET',
			header:option.header || {
				"Cookie": uni.getStorageSync('cookie'),
				'content-type': "application/json;charset=UTF-8", //默认请求头
			},
			timeout:option.timeout || 10000,
			data:option.data || {},
			success:res=>{
				switch (res.statusCode){
					//成功
					case '200':
						resolve(res)
						break;
					//超时
					case '0006':
						uni.showToast({
					        title: "登录状态已失效",
					        icon: "none",
					        duration: 2000,
					    });
						//清除所有缓存
						// uni.clearStorageSync();
						uni.reLaunch({
						  url: "/pages/login/index?loginType=0006",
						});
						break;
					default:
						resolve(res)
						break;
				}
			},
			fail:error=>{
				uni.showToast({
					title: "请求失败，请稍后再试",
					icon: "none",
				})
				reject(err)
			},
			//不管成功与否都会执行
			complete:()=>{
				uni.hideToast()
			}
		})
	})
}
export default request